//
//  ImageLayer.m
//  Agility
//
//  Created by Dominique d'Argent on 16.03.11.
//  Copyright 2011 Nicky Nubbel. All rights reserved.
//

#import "ImageLayer.h"


@implementation ImageLayer

@synthesize imageName;

#pragma mark - Object lifecycle

//
// initWithImageNamed:
//
// Creates a new layer with an image specified by its name.
//
- (id)initWithImageNamed:(NSString *)newImageName {
    self = [self init];
    
    if (self) {
        self.imageName = newImageName;
    }
    
    return self;
}

//
// dealloc
//
// Releases retained objects.
//
- (void)dealloc {
    [imageName release];
    
    [super dealloc];
}

#pragma mark - Property accessors.

//
// setImageName:
//
// Override of property accessor. When the image name changes, a new image is
// loaded and the layer's bounds are set to the new image's size.
//
- (void)setImageName:(NSString *)newImageName {
    if (![newImageName isEqualToString:imageName]) {
        [imageName release];
        
        imageName = [newImageName copy];
        
        CGImageRef image = [UIImage imageNamed:imageName].CGImage;

        NSUInteger width  = CGImageGetWidth(image);
        NSUInteger height = CGImageGetHeight(image);
        
        self.bounds = CGRectMake(0, 0, width, height);
        self.contents = (id)image;
    }
}

@end